//
//  ParallaxBackground.m
//  ScrollingWithJoy
//
//  Created by Steffen Itterheim on 11.08.10.
//  Copyright 2010 Steffen Itterheim. All rights reserved.
//

#import "ParallaxBackground.h"


@implementation ParallaxBackground

-(id) init
{
	if ((self = [super init]))
	{
		// The screensize never changes during gameplay, so we can cache it in a member variable.
		screenSize = [[CCDirector sharedDirector] winSize];
		
        NSString * str_bg_name=nil;
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            str_bg_name=@"Background_doodle-hd.png";
        }
        else{
            str_bg_name=@"Background_doodle.png";
        }
        
		NSString* Name = [NSString stringWithFormat:str_bg_name];
		CCSprite* sprite = [CCSprite spriteWithFile:Name];
		sprite.anchorPoint = CGPointMake(0, 0.5f);
		sprite.position = CGPointMake(0, screenSize.height / 2);
		[self addChild:sprite];
		
		/*
		CCSprite * bg=[CCSprite spriteWithSpriteFrameName:@"bush1.png"];  //bush1.png
		bg.anchorPoint=ccp(0,0);
		//bg.position=ccp(winSize.width/2,winSize.height/2);
		bg.position=ccp(0,0);
		//bg.color=ccc3(100, 100, 100);
		[self addChild:bg z:0 tag:100];
		
		
		CCSprite * bg2=[CCSprite spriteWithSpriteFrameName:@"bush2.png"];  //bush2.png
		bg2.anchorPoint=ccp(1,0);
		//bg.position=ccp(winSize.width/2,winSize.height/2);
		bg2.position=ccp(screenSize.width,0);
		//bg.color=ccc3(100, 100, 100);
		[self addChild:bg2 z:0 tag:100];
		*/
		return self; ////不做背景了，太慢了
		
		

		
		// Get the game's texture atlas texture by adding it. Since it's added already it will simply return 
		// the CCTexture2D associated with the texture atlas.
		CCTexture2D* gameArtTexture = [[CCTextureCache sharedTextureCache] addImage:@"game-art.png"];
		
		// Create the background spritebatch
		spriteBatch = [CCSpriteBatchNode batchNodeWithTexture:gameArtTexture];
		[self addChild:spriteBatch];

		numStripes = 2;
		
		// Add the 7 different stripes and position them on the screen
		for (int i = 0; i < numStripes; i++)
		{
			NSString* frameName = [NSString stringWithFormat:@"bg%i.png", i];
			CCSprite* sprite = [CCSprite spriteWithSpriteFrameName:frameName];
			sprite.anchorPoint = CGPointMake(0, 0.5f);
			sprite.position = CGPointMake(0, screenSize.height / 2);
			sprite.scaleY=1.7;
			
			[spriteBatch addChild:sprite z:i tag:i];

		}

		// Add 7 more stripes, flip them and position them next to their neighbor stripe
		for (int i = 0; i < numStripes; i++)
		{
			NSString* frameName = [NSString stringWithFormat:@"bg%i.png", i];
			CCSprite* sprite = [CCSprite spriteWithSpriteFrameName:frameName];
			
			// Position the new sprite one screen width to the right
			sprite.anchorPoint = CGPointMake(0, 0.5f);
			sprite.position = CGPointMake(screenSize.width - 1, screenSize.height / 2);
			sprite.scaleY=1.7;
			
			// Flip the sprite so that it aligns perfectly with its neighbor
			sprite.flipX = YES;

			// Add the sprite using the same tag offset by numStripes
			[spriteBatch addChild:sprite z:i tag:i + numStripes];
		}
		
		// Initialize the array that contains the scroll factors for individual stripes.
		speedFactors = [[CCArray alloc] initWithCapacity:numStripes];
		[speedFactors addObject:[NSNumber numberWithFloat:0.3f]];
		[speedFactors addObject:[NSNumber numberWithFloat:0.5f]];
		/*
		[speedFactors addObject:[NSNumber numberWithFloat:0.5f]];
		[speedFactors addObject:[NSNumber numberWithFloat:0.8f]];
		[speedFactors addObject:[NSNumber numberWithFloat:0.8f]];
		[speedFactors addObject:[NSNumber numberWithFloat:1.2f]];
		[speedFactors addObject:[NSNumber numberWithFloat:1.2f]];
		 */
		NSAssert([speedFactors count] == numStripes, @"speedFactors count does not match numStripes!");

		scrollSpeed = 1.0f;
		[self scheduleUpdate];
	}
	
	return self;
}

-(void) dealloc
{
	[speedFactors release];
	[super dealloc];
}

-(void) update:(ccTime)delta
{
	CCSprite* sprite;
	CCARRAY_FOREACH([spriteBatch children], sprite)
	{
		//CCLOG(@"tag: %i", sprite.tag);
		NSNumber* factor = [speedFactors objectAtIndex:sprite.zOrder];
		
		CGPoint pos = sprite.position;
		pos.x -= scrollSpeed * [factor floatValue];
		
		// Reposition stripes when they're out of bounds
		if (pos.x < -screenSize.width)
		{
			pos.x += (screenSize.width * 2) - 2;
		}
		
		sprite.position = pos;
	}
}

@end
